"""

Task that displays a series of graphs in the DEXEN Graphical User Interface.

"""

# Import schema modules
from tasks.operators import gui_operators
from tasks.libs import constants as sc

# Import DEXEN modules
from dexen.user.libs import constants as dc
from dexen.user.libs import server_api

""" GLOBALS """
pop_api = server_api.PopAPIFactory()

# The birth number is used as a timer for the graphs. In order to start the 
# graphs at the beginning of the evolutionary process, this timer needs to 
# be set to 0.

timer = 0

def gui():
    """
    Task that:
        - downloads individuals from the server
        - adds data to each graph
    """
    print "I am in gui" 
    
    # Increment the timer to get the next set of individuals to add to the graph.
    global timer
    timer += 200    
    
    # For the objective graphs and the pareto graphs, individuals need to be 
    # downloaded. Note that the 'at_time' argument is used, so the individuals
    # that are downloaded are those that were alive at the  specified time.
    individuals = pop_api.downloadLiveIndividuals(
        select=(sc.WIDTH, sc.HEIGHT), 
        where=(
            (sc.WIDTH, dc.NOT_EQUAL, None),
            (sc.HEIGHT, dc.NOT_EQUAL, None),
        ), 
        at_time=timer,
        exclusive_use=False
    )
    
    # Apply the operators to add the data to all the graphs
    gui_operators.updatePopulationGraph(time)
    gui_operators.updateObjectiveGraph1(individuals, time)
    gui_operators.updateObjectiveGraph2(individuals, time)
    gui_operators.updateParetoGraph(individuals)

        